
const getSelectedVarsName = function(store) {
  let selectedName = '';
  const selectedVars = store.state.matlab.selectedVars;
  selectedVars.forEach((variable, index) => {
    if (index !== 0) {
      selectedName += ',';
    }
    selectedName += variable.Name;
  });
  return selectedName;
};

export const plot = function(store, type) {
  const selectedVars = store.state.matlab.selectedVars;
  if (selectedVars.length === 1) {
    switch (type) {
      case 'histogram': store.commit('matlab/termCall', `pangu.plot.histogram(${selectedVars[0].Name})`); break;
      case 'pie': store.commit('matlab/termCall', `pangu.plot.pie(${selectedVars[0].Name})`); break;
    }
  } else if (selectedVars.length === 2) {
    switch (type) {
      case 'plot':store.commit('matlab/termCall', `pangu.plot.plot(${selectedVars[0].Name}, ${selectedVars[1].Name})`); break;
      case 'semilogx':store.commit('matlab/termCall', `pangu.plot2.semilogx(${selectedVars[0].Name}, ${selectedVars[1].Name})`); break;
      case 'semilogy':store.commit('matlab/termCall', `pangu.plot2.semilogy(${selectedVars[0].Name}, ${selectedVars[1].Name})`); break;
      case 'loglog':store.commit('matlab/termCall', `pangu.plot2.loglog(${selectedVars[0].Name}, ${selectedVars[1].Name})`); break;
      case 'scatter':store.commit('matlab/termCall', `pangu.plot.scatter(${selectedVars[0].Name}, ${selectedVars[1].Name})`); break;
      case 'area':store.commit('matlab/termCall', `pangu.plot.area(${selectedVars[0].Name}, ${selectedVars[1].Name})`); break;
      case 'bar':store.commit('matlab/termCall', `pangu.plot.bar(${selectedVars[0].Name}, ${selectedVars[1].Name})`); break;
      case 'barh':store.commit('matlab/termCall', `pangu.plot2.barh(${selectedVars[0].Name}, ${selectedVars[1].Name})`); break;
      case 'barstack':store.commit('matlab/termCall', `pangu.plot.bar(${selectedVars[0].Name}, ${selectedVars[1].Name}, 'stack')`); break;
      case 'barhstack':store.commit('matlab/termCall', `pangu.plot2.barh(${selectedVars[0].Name}, ${selectedVars[1].Name}, 'stack')`); break;
      case 'errorbar':store.commit('matlab/termCall', `pangu.plot2.errorbar(${selectedVars[0].Name}, ${selectedVars[1].Name})`); break;
      case 'errorbarh':store.commit('matlab/termCall', `pangu.plot2.errorbarh(${selectedVars[0].Name}, ${selectedVars[1].Name})`); break;
    }
  } else if (selectedVars.length === 3) {
    switch (type) {
      case 'plot3d':store.commit('matlab/termCall', `pangu.plot.line3d(${selectedVars[0].Name}, ${selectedVars[1].Name}, ${selectedVars[2].Name})`); break;
      case 'scatter3d':store.commit('matlab/termCall', `pangu.plot.scatter3d(${selectedVars[0].Name}, ${selectedVars[1].Name}, ${selectedVars[2].Name})`); break;
    }
  } else {
    console.error('situation 4');
  }
  // store.commit('matlab/showMatlabImg', { json: JSON.stringify(fig_json), isShow: true });
};

export const getDrawList = function(store, keyWord) {
  // console.log(process.env);
  const useableList = [
    [],
    ['histogram', 'pie'],
    ['plot', 'semilogx', 'semilogy', 'loglog', 'scatter', 'area', 'bar', 'barh', 'barstack', 'barhstack', 'errorbar', 'errorbarh'],
    ['plot3d', 'scatter3d']
  ]; // 当前选择的变量对应能应用的函数列表
  const selectedVarsName = getSelectedVarsName(store);
  var baseUrl = process.env.BASE_URL;
  // var baseUrl = 'http://120.77.147.7';
  var fulldrawList = [
    {
      name: '线图',
      useable: true,
      list: [
        {
          picture: require('@/assets/images/plottype-plot.png'),
          shortname: 'plot',
          name: 'plot' + '(' + selectedVarsName + ')',
          intro: '使用线性坐标区的二维线图',
          type: 'plot',
          wiki: baseUrl + '/wiki/function/plot/plot/',
          useable: true // 当前选择的变量是否能应用于该函数
        },
        {
          picture: require('@/assets/images/plottype-semilogx.png'),
          shortname: 'semilogx',
          name: 'semilogx' + '(' + selectedVarsName + ')',
          intro: '半对数刻度图',
          type: 'semilogx',
          wiki: baseUrl + '/wiki/function/plot2/semilogx/',
          useable: true
        },
        {
          picture: require('@/assets/images/plottype-semilogy.png'),
          shortname: 'semilogy',
          name: 'semilogy' + '(' + selectedVarsName + ')',
          intro: '半对数刻度图',
          type: 'semilogy',
          wiki: baseUrl + '/wiki/function/plot2/semilogy/',
          useable: true
        },
        {
          picture: require('@/assets/images/plottype-loglog.png'),
          shortname: 'loglog',
          name: 'loglog' + '(' + selectedVarsName + ')',
          intro: '双对数刻度图',
          type: 'loglog',
          wiki: baseUrl + '/wiki/function/plot2/loglog/',
          useable: true
        },
        {
          picture: require('@/assets/images/plottype-area.png'),
          shortname: 'area',
          name: 'area' + '(' + selectedVarsName + ')',
          intro: '填充区域图',
          type: 'area',
          wiki: baseUrl + '/wiki/function/plot/area/',
          useable: true
        },
        {
          picture: require('@/assets/images/plottype-error_bar.png'),
          shortname: 'errorbar',
          name: 'errorbar' + '(' + selectedVarsName + ')',
          intro: '误差条形图',
          type: 'errorbar',
          wiki: baseUrl + '/wiki/function/plot2/errorbar/',
          useable: true
        },
        {
          picture: require('@/assets/images/plottype-error_barh.png'),
          shortname: 'errorbarh',
          name: 'errorbarh' + '(' + selectedVarsName + ')',
          intro: '水平误差条形图',
          type: 'errorbarh',
          wiki: baseUrl + '/wiki/function/plot2/errorbarh/',
          useable: true
        },
        {
          picture: require('@/assets/images/plottype-plot3d.png'),
          shortname: 'plot3d',
          name: 'plot3d' + '(' + selectedVarsName + ')',
          intro: '使用线性坐标区的三维线图',
          type: 'plot3d',
          wiki: baseUrl + '/wiki/function/plot/line3d/',
          useable: true
        }
      ]
    },
    {
      name: '条形图',
      useable: true,
      list: [
        {
          picture: require('@/assets/images/plottype-bar.png'),
          shortname: 'bar',
          name: 'bar' + '(' + selectedVarsName + ')',
          intro: '条形图',
          type: 'bar',
          wiki: baseUrl + '/wiki/function/plot/bar/',
          useable: true
        },
        {
          picture: require('@/assets/images/plottype-barh.png'),
          shortname: 'barh',
          name: 'barh' + '(' + selectedVarsName + ')',
          intro: '水平条形图',
          type: 'barh',
          wiki: baseUrl + '/wiki/function/plot2/barh/',
          useable: true
        },
        {
          picture: require('@/assets/images/plottype-bar-stack.png'),
          shortname: 'bar',
          name: 'bar(堆叠)',
          intro: '垂直堆叠条形图',
          type: 'barstack',
          wiki: baseUrl + '/wiki/function/plot/bar/',
          useable: true
        },
        {
          picture: require('@/assets/images/plottype-barh-stack.png'),
          shortname: 'barh',
          name: 'barh(堆叠)',
          intro: '水平堆叠条形图',
          type: 'barhstack',
          wiki: baseUrl + '/wiki/function/plot2/barh/',
          useable: true
        },
        {
          picture: require('@/assets/images/plottype-histogram.png'),
          shortname: 'histogram',
          name: 'histogram' + '(' + selectedVarsName + ')',
          intro: '直方图',
          type: 'histogram',
          wiki: baseUrl + '/wiki/function/plot/histogram/',
          useable: true
        }
      ]
    },
    {
      name: '散点图',
      useable: true,
      list: [
        {
          picture: require('@/assets/images/plottype-scatter.png'),
          shortname: 'scatter',
          name: 'scatter' + '(' + selectedVarsName + ')',
          intro: '散点/气泡图',
          type: 'scatter',
          wiki: baseUrl + '/wiki/function/plot/scatter/',
          useable: true
        },
        {
          picture: require('@/assets/images/plottype-scatter3d.png'),
          shortname: 'scatter3d',
          name: 'scatter3d' + '(' + selectedVarsName + ')',
          intro: '三维散点图',
          type: 'scatter3d',
          wiki: baseUrl + '/wiki/function/plot/scatter3d/',
          useable: true
        }
      ]
    },
    {
      name: '饼图',
      useable: true,
      list: [
        {
          picture: require('@/assets/images/plottype-pie.png'),
          shortname: 'pie',
          name: 'pie' + '(' + selectedVarsName + ')',
          intro: '饼图',
          type: 'pie',
          wiki: baseUrl + '/wiki/function/plot/pie/',
          useable: true
        }
      ]
    }
    // {
    //   name: '地理图',
    //   useable: true,
    //   list: [
    //     {
    //       picture: require('@/assets/images/plottype-plot.png'),
    //       shortname: 'geoplot',
    //       name: 'geoplot' + '(' + selectedVarsName + ')',
    //       intro: '地理坐标图',
    //       type: 'line',
    //       wiki: baseUrl + '/wiki/function/plot/plot/',
    //       useable: true
    //     },
    //     {
    //       picture: require(' @/assets/images/plottype-plot.png'),
    //       shortname: 'geoscatter',
    //       name: 'geoscatter' + '(' + selectedVarsName + ')',
    //       intro: '地理散点图',
    //       type: 'line',
    //       wiki: baseUrl + '/wiki/function/plot/plot/',
    //       useable: true
    //     },
    //     {
    //       picture: require('@/assets/images/plottype-plot.png'),
    //       shortname: 'geobubble',
    //       name: 'geobubble' + '(' + selectedVarsName + ')',
    //       intro: '地理气泡图',
    //       type: 'line',
    //       wiki: baseUrl + '/wiki/function/plot/plot/',
    //       useable: true
    //     }
    //   ]
    // },
    // {
    //   name: '等高线图',
    //   useable: true,
    //   list: [
    //     {
    //       picture: require('@/assets/images/plottype-plot.png'),
    //       shortname: 'contour',
    //       name: 'contour' + '(' + selectedVarsName + ')',
    //       intro: '等高线图',
    //       type: 'line',
    //       wiki: baseUrl + '/wiki/function/plot/plot/',
    //       useable: true
    //     },
    //     {
    //       picture: require('@/assets/images/plottype-plot.png'),
    //       shortname: 'contourf',
    //       name: 'contourf' + '(' + selectedVarsName + ')',
    //       intro: '填充的等高线图',
    //       type: 'line',
    //       wiki: baseUrl + '/wiki/function/plot/plot/',
    //       useable: true
    //     }
    //   ]
    // },
    // {
    //   name: '三维曲面图',
    //   useable: true,
    //   list: [
    //     {
    //       picture: require('@/assets/images/plottype-plot.png'),
    //       shortname: 'surf',
    //       name: 'surf' + '(' + selectedVarsName + ')',
    //       intro: '将矩阵显示为曲面',
    //       type: 'line',
    //       wiki: baseUrl + '/wiki/function/plot/plot/',
    //       useable: true
    //     },
    //     {
    //       picture: require('@/assets/images/plottype-plot.png'),
    //       shortname: 'mesh',
    //       name: 'mesh' + '(' + selectedVarsName + ')',
    //       intro: '三维网格曲面',
    //       type: 'line',
    //       wiki: baseUrl + '/wiki/function/plot/plot/',
    //       useable: true
    //     }
    //   ]
    // }
  ];

  // 根据关键词搜索
  var drawList = [];
  if (keyWord === '') {
    drawList = fulldrawList;
  } else {
    for (let i = 0; i < fulldrawList.length; ++i) {
      const type = fulldrawList[i];
      const list = type.list;
      type.list = [];
      for (let j = 0; j < list.length; ++j) {
        const text = list[j].shortname + list[j].intro;
        if (text.includes(keyWord)) {
          type.list.push(list[j]);
        }
      }
      if (type.list.length !== 0) {
        drawList.push(type);
      }
    }
  }

  // 判断当前选择变量可应用的函数
  const varNum = store.state.matlab.selectedVars.length;
  for (let i = 0; i < drawList.length; ++i) {
    const arr = drawList[i].list;
    let tag = true;
    for (let j = 0; j < arr.length; ++j) {
      if (varNum > 3 || useableList[varNum].indexOf(arr[j].type) === -1) {
        drawList[i].list[j].useable = false;
      } else {
        tag = false;
      }
    }
    if (tag) {
      drawList[i].useable = false;
    }
  }

  return drawList;
};
